package com.hds.modules.app.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hds.common.utils.PageUtils;
import com.hds.common.utils.Query;
import com.hds.modules.app.dao.ActivationCodeDao;
import com.hds.modules.app.entity.ActivationCodeEntity;
import com.hds.modules.app.service.ActivationCodeService;
import com.hds.modules.sys.dao.base.SysCompanyDao;
import com.hds.modules.sys.dao.base.SysMemberShipDao;
import com.hds.modules.sys.entity.base.SysCompanyEntity;
import com.hds.modules.sys.entity.base.SysMemberShipEntity;
import com.hds.modules.sys.entity.dto.ActivationCodeDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author guonan
 * @Email guonan@yewcn.com
 * @Description
 * @Date 2018/6/11 10:28
 */
@Service("activationCodeService")
public class ActivationCodeServiceImpl extends ServiceImpl<ActivationCodeDao, ActivationCodeEntity> implements ActivationCodeService{

    @Autowired
    private ActivationCodeDao activationCodeDao;

    @Autowired
    private SysMemberShipDao shipDao;

    @Autowired
    private SysCompanyDao companyDao;

    @Override
    public List<ActivationCodeEntity> queryByCompanyId(Integer companyId){

        return  activationCodeDao.queryByCompanyId(companyId);
    }

    @Override
    public String queryMaxCardNo(Integer companyId){
        return activationCodeDao.queryMaxCardNo(companyId);
    }

    @Override
    public ActivationCodeEntity queryByCode(String code) {
        return activationCodeDao.queryByCode(code);
    }

    @Override
    public Integer queryMaxBatchByCompanyId(Integer companyId) {
        return activationCodeDao.queryMaxBatchByCompanyId(companyId);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");

        Page<ActivationCodeEntity> page = this.selectPage(
                new Query<ActivationCodeEntity>(params).getPage(),
                new EntityWrapper<ActivationCodeEntity>().like(StringUtils.isNotBlank(key),"username", key)
        );
        List<ActivationCodeEntity> records = page.getRecords();
        for (ActivationCodeEntity code: records) {

            SysCompanyEntity company = companyDao.selectById(code.getCompanyId());

            String cardPrefix=null,levelPrefix=null;
            if(company != null){
                cardPrefix = company.getCardPrefix();
            }

            SysMemberShipEntity ship = shipDao.selectById(code.getLevelId());
            if(ship != null){
                levelPrefix = ActivationCodeEntity.LEVELPREFIX + ship.getLevel();
            }
            code.setCardNo(cardPrefix + levelPrefix + code.getCardNo());
            code.setCompany(company);
            code.setShip(ship);

        }
        page.setRecords(records);

        return new PageUtils(page);
    }


}
